$(document).ready(function () {
    showInfo(arr,i);

    $(".order").on("click",function () {
        $.ajax({
            "url":"../order",
            "type":"POST",
            "data":{"method":"createOrder","arr":JSON.stringify(arr)},
            "success":function (result) {
                alert(result)
                if(result=='true'){
                    alert("生成订单成功！");
                    location.reload();
                }else{
                    location.href="/xiaomi/addAddress.html";
                }
            }
        })
    })


})

function showInfo(arr,i) {
    $.ajax({
        "url":"../shopcar.do",
        "type":"POST",
        "data":{"method":"findAllInfo"},
        "dataType":"json",
        "success":function (result) {
            $.each(result,function (index,data) {
                $("table").append(addInfo(data));
            })
            $(".checkOne").on("click",function () {
                checkInfo(arr,i);
            })
        }
    })
}
function addInfo(data) {
    return $("<tr><td><input type='checkbox' class='checkOne' value='"+data.id+"'></td>" +
        "<td>"+data.goodsName+"</td>" +
        "<td>"+data.price+"</td>" +
        "<td>"+data.count+"</td>" +
        "<td><a class='delete' href='javascript:deleteInfo("+data.id+")'>删除</a></td>" +
        "</tr>")
}

function checkInfo() {
    $(".checkOne").each(function () {
        if($(this).prop("checked")==true){
            arr[i] = $(this).val();
        }
    })
}

function deleteInfo(id) {
    $.ajax({
        "url":"../shopcar.do",
        "type":"POST",
        "data":{"method":"deleteInfo","id":id},
        "success":function (result) {
            if(result=='true'){
                alert("删除成功");
                location.reload();
            }else{
                alert("删除失败");
            }
        }
    })
}